// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.characters.WornItems;

import java.util.ArrayList;
import java.util.function.Consumer;
import zombie.core.skinnedmodel.visual.ItemVisual;
import zombie.core.skinnedmodel.visual.ItemVisuals;
import zombie.inventory.InventoryItem;
import zombie.inventory.InventoryItemFactory;
import zombie.inventory.ItemContainer;
import zombie.inventory.types.Clothing;
import zombie.inventory.types.InventoryContainer;
import zombie.util.StringUtils;

public final class WornItems {
    protected final BodyLocationGroup group;
    protected final ArrayList<WornItem> items = new ArrayList();

    public WornItems(BodyLocationGroup _group) {
        this.group = _group;
    }

    public WornItems(WornItems other) {
        this.group = other.group;
        this.copyFrom(other);
    }

    public void copyFrom(WornItems other) {
        if (this.group != other.group) {
            throw new RuntimeException("group=" + this.group.id + " other.group=" + other.group.id);
        } else {
            this.items.clear();
            this.items.addAll(other.items);
        }
    }

    public BodyLocationGroup getBodyLocationGroup() {
        return this.group;
    }

    public WornItem get(int index) {
        return (WornItem)this.items.get(index);
    }

    public void setItem(String location, InventoryItem item) {
        this.group.checkValid(location);
        if (!this.group.isMultiItem(location)) {
            int int0 = this.indexOf(location);
            if (int0 != -1) {
                this.items.remove(int0);
            }
        }

        for (int int1 = 0; int1 < this.items.size(); int1++) {
            WornItem wornItem0 = (WornItem)this.items.get(int1);
            if (this.group.isExclusive(location, wornItem0.location)) {
                this.items.remove(int1--);
            }
        }

        if (item != null) {
            this.remove(item);
            int int2 = this.items.size();

            for (int int3 = 0; int3 < this.items.size(); int3++) {
                WornItem wornItem1 = (WornItem)this.items.get(int3);
                if (this.group.indexOf(wornItem1.getLocation()) > this.group.indexOf(location)) {
                    int2 = int3;
                    break;
                }
            }

            WornItem wornItem2 = new WornItem(location, item);
            this.items.add(int2, wornItem2);
        }
    }

    public InventoryItem getItem(String location) {
        this.group.checkValid(location);
        int _int = this.indexOf(location);
        return _int == -1 ? null : ((WornItem)this.items.get(_int)).item;
    }

    public InventoryItem getItemByIndex(int index) {
        return index >= 0 && index < this.items.size() ? ((WornItem)this.items.get(index)).getItem() : null;
    }

    public void remove(InventoryItem item) {
        int _int = this.indexOf(item);
        if (_int != -1) {
            this.items.remove(_int);
        }
    }

    public void clear() {
        this.items.clear();
    }

    public String getLocation(InventoryItem item) {
        int _int = this.indexOf(item);
        return _int == -1 ? null : ((WornItem)this.items.get(_int)).getLocation();
    }

    public boolean contains(InventoryItem item) {
        return this.indexOf(item) != -1;
    }

    public int size() {
        return this.items.size();
    }

    public boolean isEmpty() {
        return this.items.isEmpty();
    }

    public void forEach(Consumer<WornItem> c) {
        for (int _int = 0; _int < this.items.size(); _int++) {
            c.accept((WornItem)this.items.get(_int));
        }
    }

    public void setFromItemVisuals(ItemVisuals itemVisuals) {
        this.clear();

        for (int _int = 0; _int < itemVisuals.size(); _int++) {
            ItemVisual itemVisual = (ItemVisual)itemVisuals.get(_int);
            String string = itemVisual.getItemType();
            InventoryItem inventoryItem = InventoryItemFactory.CreateItem(string);
            if (inventoryItem != null) {
                if (inventoryItem.getVisual() != null) {
                    inventoryItem.getVisual().copyFrom(itemVisual);
                    inventoryItem.synchWithVisual();
                }

                if (inventoryItem instanceof Clothing && !StringUtils.isNullOrWhitespace(inventoryItem.getBodyLocation())) {
                    this.setItem(inventoryItem.getBodyLocation(), inventoryItem);
                } else if (inventoryItem instanceof InventoryContainer && !StringUtils.isNullOrWhitespace(((InventoryContainer)inventoryItem).canBeEquipped())) {
                    this.setItem(((InventoryContainer)inventoryItem).canBeEquipped(), inventoryItem);
                }
            }
        }
    }

    public void getItemVisuals(ItemVisuals itemVisuals) {
        itemVisuals.clear();

        for (int _int = 0; _int < this.items.size(); _int++) {
            InventoryItem inventoryItem = ((WornItem)this.items.get(_int)).getItem();
            ItemVisual itemVisual = inventoryItem.getVisual();
            if (itemVisual != null) {
                itemVisual.setInventoryItem(inventoryItem);
                itemVisuals.add(itemVisual);
            }
        }
    }

    public void addItemsToItemContainer(ItemContainer container) {
        for (int int0 = 0; int0 < this.items.size(); int0++) {
            InventoryItem inventoryItem = ((WornItem)this.items.get(int0)).getItem();
            int int1 = inventoryItem.getVisual().getHolesNumber();
            inventoryItem.setCondition(inventoryItem.getConditionMax() - int1 * 3);
            container.AddItem(inventoryItem);
        }
    }

    private int indexOf(String string) {
        for (int _int = 0; _int < this.items.size(); _int++) {
            WornItem wornItem = (WornItem)this.items.get(_int);
            if (wornItem.location.equals(string)) {
                return _int;
            }
        }

        return -1;
    }

    private int indexOf(InventoryItem inventoryItem) {
        for (int _int = 0; _int < this.items.size(); _int++) {
            WornItem wornItem = (WornItem)this.items.get(_int);
            if (wornItem.getItem() == inventoryItem) {
                return _int;
            }
        }

        return -1;
    }
}
